package com.broad.commerce.code.services.base;

import com.broad.commerce.code.base.EntityModuleCreator;
import com.broad.commerce.code.models.EntityModuleDTO;
import com.broad.commerce.code.modules.BusinessModule;
import com.broad.commerce.code.modules.jdbc.EntityModule;
import com.broad.commerce.code.services.ProjectRootDirService;
import com.broad.commerce.code.services.business.ControllerModuleService;
import com.broad.commerce.code.services.business.InterfaceModuleService;
import com.broad.commerce.code.services.business.ServiceModuleService;
import com.broad.commerce.web.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-02-09 00:06
 * @description: BusinessModuleService
 **/
@Service
public class BusinessModuleService {
	@Autowired
	private InterfaceModuleService interfaceModuleService;
	@Autowired
	private ServiceModuleService serviceModuleService;
	@Autowired
	private ControllerModuleService controllerModuleService;

	public void createModuleFiles(EntityModuleDTO entityModuleDTO) {
		String rootPath = ProjectRootDirService.PROJECT_DIR_MAP.get(entityModuleDTO.getBusinessProject());
		String filePath = rootPath + "/src/main/java/";
		BusinessModule businessModule = this.initBusinessModule(entityModuleDTO);
		this.interfaceModuleService.createClassFile(businessModule.getInterfaceModule(), filePath, "java");
		this.serviceModuleService.createClassFile(businessModule.getServiceModule(), filePath, "java");
		this.controllerModuleService.createClassFile(businessModule.getControllerModule(), filePath, "java");
	}

	public BusinessModule initBusinessModule(EntityModuleDTO entityModuleDTO) {
		EntityModule entityModule = EntityModuleCreator.createEntityModule(entityModuleDTO);
		String entityPackage = entityModuleDTO.getPackageName();
		String servicePackage = entityModuleDTO.getServicePackage();
		if (!entityPackage.contains("entity")) {
			throw new ServiceException("实体类包名中必须含有 entity 字段！");
		}
		if (!servicePackage.contains("services")) {
			throw new ServiceException("服务类包名中必须含有 services 字段！");
		}
		String modelPackage = entityPackage.replace("entity", "model");
		String jpaPackage = entityPackage.replace("entity", "jpa");
		String mybatisPackage = entityPackage.replace("entity", "mybatis");
		String fastMybatisPackage = entityPackage.replace("entity", "fastmybatis");
		String interfacePackage = servicePackage.replace("services", "interfaces");
		String controllerPackage = servicePackage.replace("services", "controller");
		//@off
		BusinessModule businessModule = BusinessModule.builder()
		                                              .entityModule(entityModule)
			                                          .modelPackage(modelPackage)
		                                              .jpaPackage(jpaPackage)
		                                              .mybatisPackage(mybatisPackage)
			                                          .interfacePackage(interfacePackage)
			                                          .servicePackage(servicePackage)
			                                          .controllerPackage(controllerPackage)
		                                              .build();//@on
		businessModule.initBusinessModule(entityModule.getMapping());
		return businessModule;
	}
}
